package com.ruoyi.web.bst;

import java.util.Collections;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ruoyi.bst.area.service.AreaValidator;
import com.ruoyi.bst.areaJoin.domain.enums.AreaJoinPermission;
import com.ruoyi.bst.credit.domain.Credit;
import com.ruoyi.bst.credit.domain.CreditQuery;
import com.ruoyi.bst.credit.domain.CreditVO;
import com.ruoyi.bst.credit.service.CreditConverter;
import com.ruoyi.bst.credit.service.CreditService;
import com.ruoyi.bst.credit.service.CreditValidator;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.validate.ValidGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ServiceUtil;
import com.ruoyi.common.utils.collection.CollectionUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;

/**
 * 挂账方Controller
 *
 * @author ruoyi
 * @date 2025-05-27
 */
@RestController
@RequestMapping("/bst/credit")
public class CreditController extends BaseController
{
    @Autowired
    private CreditService creditService;
    
    @Autowired
    private AreaValidator areaValidator;

    @Autowired
    private CreditConverter creditConverter;

    @Autowired
    private CreditValidator creditValidator;

    /**
     * 查询挂账方列表
     */
    @PreAuthorize("@ss.hasPermi('bst:credit:list')")
    @GetMapping("/list")
    public TableDataInfo list(CreditQuery query)
    {
        startPage();
        startOrderBy();
        query.setScope(true);
        query.addAreaPermission(AreaJoinPermission.CREDIT_VIEW.getCode());
        List<CreditVO> list = creditService.selectCreditList(query);
        return getDataTable(list);
    }

    /**
     * 通过ID列表查询简单数据
     */
    @PreAuthorize("@ss.hasPermi('bst:credit:list')")
    @PostMapping("/simpleListByIds")
    public AjaxResult simpleListByIds(@RequestBody List<Long> ids)
    {
        if (CollectionUtils.isEmptyElement(ids)) {
            return success(Collections.emptyList());
        }
        CreditQuery query = new CreditQuery();
        query.setIds(ids);
        query.setScope(true);
        query.addAreaPermission(AreaJoinPermission.CREDIT_VIEW.getCode());
        List<CreditVO> list = creditService.selectCreditList(query);
        return success(list);
    }

    /**
     * 导出挂账方列表
     */
    @PreAuthorize("@ss.hasPermi('bst:credit:export')")
    @Log(title = "挂账方", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CreditQuery query)
    {
        query.setScope(true);
        query.addAreaPermission(AreaJoinPermission.CREDIT_VIEW.getCode());
        List<CreditVO> list = creditService.selectCreditList(query);
        ExcelUtil<CreditVO> util = new ExcelUtil<CreditVO>(CreditVO.class);
        util.exportExcel(response, list, "挂账方数据");
    }

    /**
     * 获取挂账方详细信息
     */
    @PreAuthorize("@ss.hasPermi('bst:credit:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(creditService.selectCreditById(id, true));
    }

    /**
     * 新增挂账方
     */
    @PreAuthorize("@ss.hasPermi('bst:credit:add')")
    @Log(title = "挂账方", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody @Validated(ValidGroup.Create.class) CreditVO credit) {
        if (credit.getAreaId() != null) {
            ServiceUtil.assertion(!areaValidator.canEdit(credit.getAreaId()), "您无权操作ID为%s的运营区", credit.getAreaId());
        }
        credit.setCreateId(getUserId());
        Credit po = creditConverter.toPoByCreate(credit);
        return toAjax(creditService.insertCredit(po));
    }

    /**
     * 修改挂账方
     */
    @PreAuthorize("@ss.hasPermi('bst:credit:edit')")
    @Log(title = "挂账方", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody @Validated(ValidGroup.Update.class) CreditVO credit){
        ServiceUtil.assertion(!creditValidator.hasPermission(credit.getId(), AreaJoinPermission.CREDIT_EDIT),
         "您无权操作ID为%s的挂账方", credit.getId());
        if (credit.getAreaId() != null) {
            ServiceUtil.assertion(!areaValidator.canEdit(credit.getAreaId()), "您无权操作ID为%s的运营区", credit.getAreaId());
        }
        Credit po = creditConverter.toPoByUpdate(credit);
        return toAjax(creditService.updateCredit(po));
    }

    /**
     * 删除挂账方
     */
    @PreAuthorize("@ss.hasPermi('bst:credit:remove')")
    @Log(title = "挂账方", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<Long> ids)
    {
        ServiceUtil.assertion(!creditValidator.hasPermission(ids, AreaJoinPermission.CREDIT_VIEW),
         "您无权操作ID为%s的挂账方", ids);
        return toAjax(creditService.logicDel(ids));
    }
}
