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

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

import cn.ezhizao.project.business.reconciliation.priceList.domain.BizEntrustOnce;
import cn.ezhizao.project.business.reconciliation.priceList.domain.BizEntrustPrice;
import cn.ezhizao.project.business.reconciliation.priceList.service.IBizEntrustPriceService;
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.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-07
 */
@RestController
@RequestMapping("/business/oncePrice")
public class BizEntrustOncePriceController extends BaseController {
    @Resource
    private IBizEntrustPriceService bizEntrustPriceService;

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

    /**
     * 导出单次价目表列表
     */
    @PreAuthorize("@ss.hasPermi('oncePrice:oncePrice:export')")
    @Log(title = "单次价目表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BizEntrustPrice bizEntrustPrice) throws NoSuchFieldException, IllegalAccessException {
        List<BizEntrustPrice> list = bizEntrustPriceService.getOnceList(bizEntrustPrice);
        List<BizEntrustOnce> onceList =new ArrayList<>();
        list.forEach(l -> {
            BizEntrustOnce once = new BizEntrustOnce();
            once.setRegion(l.getProvince() + "-" + l.getCity() + "-" + l.getDistrict());
            once.setTaskTypeName(l.getTaskTypeName());
            once.setTaskTypeDetailName(l.getTaskTypeId()==6?l.getAlterType()+"-"+l.getTaskTypeDetailName():l.getTaskTypeDetailName());
            once.setPrice(l.getPrice());
            once.setRemark(l.getRemark());
            onceList.add(once);
        });

        ExcelUtil<BizEntrustOnce> util = new ExcelUtil<BizEntrustOnce>(BizEntrustOnce.class);
        util.exportExcel(response, onceList, "单次价目表数据");
    }

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

    /**
     * 获取任务明细信息
     */
    @PreAuthorize("@ss.hasPermi('priceList:oncePrice:list')")
    @GetMapping(value = "、Detail/{id}")
    public AjaxResult getTaskTypeDetailList(@PathVariable("id") Long id) {
        return success(bizEntrustPriceService.getTaskTypeDetailList(id));
    }

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

    /**
     * 新增单次价目表
     */
    @PreAuthorize("@ss.hasPermi('oncePrice:oncePrice: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.getOnceList(bizEntrustPrice);
        if (list.size() > 0) {
            return AjaxResult.error("该价目已存在");
        }
        return toAjax(bizEntrustPriceService.save(bizEntrustPrice));
    }

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

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