package cn.ezhizao.project.business.reconciliation.priceList.controller;

import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import cn.ezhizao.project.business.reconciliation.priceList.domain.BizTaskType;
import cn.ezhizao.project.tenant.domain.Tenant;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import cn.ezhizao.framework.aspectj.lang.annotation.Log;
import cn.ezhizao.framework.aspectj.lang.enums.BusinessType;
import cn.ezhizao.project.business.reconciliation.priceList.domain.BizEntrustPrice;
import cn.ezhizao.project.business.reconciliation.priceList.service.IBizEntrustPriceService;
import cn.ezhizao.framework.web.controller.BaseController;
import cn.ezhizao.framework.web.domain.AjaxResult;
import cn.ezhizao.common.utils.poi.ExcelUtil;
import cn.ezhizao.framework.web.page.TableDataInfo;

/**
 * 价目表系统Controller
 *
 * @author ruoyi
 * @date 2023-12-01
 */
@RestController
@RequestMapping("/priceList/priceList")
public class BizEntrustPriceController extends BaseController {
    @Resource
    private IBizEntrustPriceService bizEntrustPriceService;

    /**
     * 查询价目表系统列表
     */
    @PreAuthorize("@ss.hasPermi('priceList:priceList:list')")
    @GetMapping("/list")
    public TableDataInfo list(BizEntrustPrice bizEntrustPrice) throws NoSuchFieldException, IllegalAccessException {
        startPage();
        bizEntrustPrice.setToTenantId(getTenantId());
        List<BizEntrustPrice> list = bizEntrustPriceService.getList(bizEntrustPrice);
        return getDataTable(list);
    }

    /**
     * 导出价目表系统列表
     */
    @PreAuthorize("@ss.hasPermi('priceList:priceList:export')")
    @Log(title = "循环价目表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BizEntrustPrice bizEntrustPrice) throws NoSuchFieldException, IllegalAccessException {
        List<BizEntrustPrice> list = bizEntrustPriceService.getList(bizEntrustPrice);
        list.forEach(l->{
            l.setAnnualIncome(l.getTaskTypeId()==1?l.getAnnualIncome():"--");
            l.setChanged((l.getTaskTypeId()==2||l.getTaskTypeId()==3)?l.getIsChanged()==0?"否":"是":"--");
        });
        ExcelUtil<BizEntrustPrice> util = new ExcelUtil<BizEntrustPrice>(BizEntrustPrice.class);
        util.exportExcel(response, list, "循环价目表数据");
    }

    /**
     * 获取价目表系统详细信息
     */
    @PreAuthorize("@ss.hasPermi('priceList:priceList:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(bizEntrustPriceService.getById(id));
    }

    /**
     * 获取租户信息
     */
    @PreAuthorize("@ss.hasPermi('priceList:priceList:list')")
    @GetMapping("/tenantList")
    public AjaxResult getTenantList(Tenant tenant) {
        return success(bizEntrustPriceService.getTenantList(tenant));
    }

    /**
     * 获取任务类型信息
     */
    @PreAuthorize("@ss.hasPermi('priceList:priceList:list')")
    @GetMapping("/taskTypeList")
    public AjaxResult getTaskTypeList() {
        return success(bizEntrustPriceService.getTaskType(1));
    }

    /**
     * 新增价目表系统
     */
    @PreAuthorize("@ss.hasPermi('priceList:priceList:save')")
    @Log(title = "价目表系统", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BizEntrustPrice bizEntrustPrice) throws NoSuchFieldException, IllegalAccessException {
        //判读是否存在该价目
        setTenantId(bizEntrustPrice);
        bizEntrustPrice.setToTenantId(getTenantId());
        List<BizEntrustPrice> list = bizEntrustPriceService.getList(bizEntrustPrice);
        if (list.size() > 0) {
            return AjaxResult.error("该价目已存在");
        }
        return toAjax(bizEntrustPriceService.save(bizEntrustPrice));
    }

    /**
     * 修改价目表系统
     */
    @PreAuthorize("@ss.hasPermi('priceList:priceList:save')")
    @Log(title = "价目表系统", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BizEntrustPrice bizEntrustPrice) throws NoSuchFieldException, IllegalAccessException {
        if(bizEntrustPrice.getIsZero()==1){
            bizEntrustPrice.setAnnualIncome(" ");
        }
        //判读是否存在该价目
        List<BizEntrustPrice> list = bizEntrustPriceService.getList(bizEntrustPrice);
        //如果存在多条数据
        if (list.size() > 1) {
            return AjaxResult.error("该价目已存在");
        } else if (list.size() == 1) {
            //如果存在一条数据且id一致
            if (list.get(0).getId().equals(bizEntrustPrice.getId())) {
                if (bizEntrustPrice.getIsZero() == 1) {
                    bizEntrustPrice.setAnnualIncome(" ");

                }
                return toAjax(bizEntrustPriceService.updateById(bizEntrustPrice));
            } else {
                return AjaxResult.error("该价目已存在");
            }
        }
        return toAjax(bizEntrustPriceService.updateById(bizEntrustPrice));
    }

    /**
     * 删除价目表系统
     */
    @PreAuthorize("@ss.hasPermi('priceList:priceList:remove')")
    @Log(title = "价目表系统", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<Long> ids) {
        return toAjax(bizEntrustPriceService.removeBatchByIds(ids));
    }

}
